﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace U9API.Pub
{
    public class FieldInfoCollection : IEnumerable //表示可遍历可枚举的集合接口
    {
        //保存Object 类型的 数组。
        List<FieldInfo> al = new List<FieldInfo>();

        private int _childcount;
        public int ChildCount
        {
            get
            {
                return this._childcount;
            }
        }
        public FieldInfoCollection(int childcount)
        {
            this._childcount = childcount;
        }
        /// <summary>
        /// 实现了IEnumberable 后，返回一个迭代器对象，但具体的接口子类由程序员
        /// 自己定义。
        /// </summary>
        /// <returns></returns>
        public IEnumerator GetEnumerator()
        {
            return new FieldInfoEnumberater(this.al);
        }

        /// <summary>
        /// 索引器
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public FieldInfo this[int index]
        {
            get { return (FieldInfo)al[index]; }
        }
        public Int32 Count
        {
            get
            {
                return this.al.Count;
            }
        }
        public void Add(FieldInfo p)
        {
            CheckFieldInfo(p);
            al.Add(p);
        }
        private void CheckFieldInfo(FieldInfo p)
        {
            if (p == null)
            {
                throw new Exception("进行FileInfoCollection操作时候，传入的FieldInfo不允许为空");
            }
            if (p.Count != this.ChildCount)
            {
                throw new Exception("进行FileInfoCollection操作时候，传入的FieldInfo的Count不允许与集合的ChildCount不一致！");
            }
        }
        public void AddRange(List<FieldInfo> ip)
        {
            if (ip == null)
            {
                throw new Exception("FileInfoCollection进行AddRange时候，传入的List<FieldInfo>不允许为空");
            }
            foreach (FieldInfo p in ip)
            {
                CheckFieldInfo(p);
            }
            al.AddRange(ip);
        }

        public void Clear()
        {
            al.Clear();
        }

        public void Insert(int index, FieldInfo value)
        {
            CheckFieldInfo(value);
            al.Insert(index, value);
        }

        public int IndexOf(FieldInfo p)
        {
            return al.IndexOf(p);
        }

        public void RemoveAt(int index)
        {
            al.RemoveAt(index);
        }

        public void Remove(FieldInfo p)
        {
            if (al.Contains(p))
            {
                int temp = IndexOf(p);
                RemoveAt(temp);
            }
        }
    }//Class_end

    public class FieldInfoEnumberater : IEnumerator
    {
        int i = 0;
        IList ps;

        public FieldInfoEnumberater(IList p)
        {
            ps = p;
        }

        /// <summary>
        /// 得到当前元素
        /// </summary>
        public object Current
        {
            get { return ps[i++]; }  //注意这里的i++ ，是先运算再自增。
            //相当于
            //object o=ps[i];
            //i++;
            //return o;

        }

        /// <summary>
        /// 移动到下一个元素
        /// </summary>
        /// <returns></returns>
        public bool MoveNext()
        {
            if (i > ps.Count - 1)
            {
                return false;
            }
            return true;
        }

        //重置
        public void Reset()
        {
            i = 0;
        }
    }


    public class FieldInfo
    {
        private Dictionary<String, String> fieldmap = new Dictionary<string, string>();
        public String this[String key]
        {
            get { return fieldmap[key]; }
        }
        int _count;
        public Int32 Count
        {
            get
            {
                return this._count;
            }
        }
        public FieldInfo(int count)
        {
            this._count = count;
        }
        public void Put(String key, String value)
        {
            if (value.IsNullOrEmpty())
            {
                value = String.Empty;
            }
            if (this.fieldmap.ContainsKey(key))
            {
                this.fieldmap[key] = value;
            }
            else if (this.fieldmap.Count < this.Count)
            {
                this.fieldmap.Add(key, value);
            }
            else
            {
                throw new Exception("当前FieldInfo的值个数为" + this._count + "，已满无法增加新参数!");
            }
        }
        public bool ContainsKey(String key)
        {
            return this.fieldmap.ContainsKey(key);
        }
        //public string fieldname;
        //public string fieldvalue;
    }
}
